#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 创建一个菜单栏
//    QMenuBar *menu = new QMenuBar();
    QMenuBar *menu = this->menuBar();
    this->setMenuBar(menu);

    // 创建菜单
    QMenu *m1 = new QMenu("文件(&S)");
    QMenu *m2 = new QMenu("编辑");
    QMenu *m3 = new QMenu("构建");
    QMenu *m4 = new QMenu("调试");

    // 创建子菜单
    QMenu *Pm = new QMenu("切换");

    // 将菜单添加到菜单栏中
    menu->addMenu(m1);
    menu->addMenu(m2);
    menu->addMenu(m3);
    menu->addMenu(m4);

    // 将子菜单添加到父菜单中
    m1->addMenu(Pm);

    // 创建菜单项
    QAction *a1 = new QAction("新建");
    QAction *a2 = new QAction("打开");
    QAction *a3 = new QAction("最近使用");
    QAction *a4 = new QAction("最近访问");
    QAction *a5 = new QAction("编辑");

    // 将菜单项添加到菜单中
    m1->addAction(a1);
    m1->addAction(a2);
    m1->addSeparator(); // 添加分割线
    m1->addAction(a3);
    m1->addAction(a4);

    // 添加菜单项到子菜单中
    Pm->addAction(a5);

    // 连接信号槽
    connect(a1, &QAction::triggered, this, &MainWindow::Handler1);
    connect(a2, &QAction::triggered, this, &MainWindow::Handler2);
    connect(a3, &QAction::triggered, this, &MainWindow::Handler4);
    connect(a4, &QAction::triggered, this, &MainWindow::Handler4);

    // 给菜单项设置图标
    a1->setIcon(QIcon(":/image/Action.png"));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Handler2()
{
    qDebug() << "触发打开";
}

void MainWindow::Handler3()
{
    qDebug() << "触发最近使用";
}

void MainWindow::Handler4()
{
    qDebug() << "触发最近访问";
}

void MainWindow::Handler1()
{
    qDebug() << "触发新建";
}

